<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2019/7/14
 * Time: 17:49
 */

namespace app\admin\controller;
use think\Db;
class House extends Common
{
    /**
     * 大棚列表
     */
    public function houseList(){
        if (request()->isPost()) {
            $key = input('post.key') ? input('key') :'';
            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('pageSize');
            $list = Db('nyd_house')
                ->where('house_name', 'like', "%" . $key . "%")
                ->paginate(array('list_rows' => $pageSize, 'page' => $page))
                ->toArray();
            foreach ($list['data'] as $k => $v){
                if($list['data'][$k]['is_bad']==1){
                    $list['data'][$k]['is_bad'] = '正常';
                }
                else if ($list['data'][$k]['is_bad']==2){
                    $list['data'][$k]['is_bad'] = '故障';
                }
            }
            return $result = ['code' => 0, 'msg' => '获取成功!', 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }
        return $this->fetch('house/house_list');
    }


    /**
     * @return mixed|void
     * tianjia
     */
    public function addhouse(){

        if(request()->isPost()){
            $data = input('post.');
            $id = $data['id'];
            $res = Db::name('nyd_house')
                ->where('id',$id)
                ->find();
            if($res){
                return $this->error("大棚已存在");
            }

            //插入
            else{
                $re = Db::name('nyd_house')->insert($data);
                if($re)
                {
                    $this->success('添加成功');
                }
                else{}
                $this->error('添加失败');
            }
        }
        else{
            return $this->fetch('house/addhouse');
        }
    }

    /**
     * @return mixed|void
     * 大棚编辑
     */
    public function houseEdit(){
        if(request()->isPost()){
            $data = input('post.');
            $re = Db::name('nyd_house')->where('id',$data['id'])->update($data);
            if($re){
                $this->success('修改成功');
            }
            else{
                $this->error('修改失败');
            }

        }
        $id = input('get.id');
        if (empty($id)){
            return $this->error('大棚不存在!');
        }
        $result = Db::name('nyd_house')->where('id',$id)->find();
        $this->assign('info',$result);
        $this->assign('id',$id);
        return $this->fetch('house/houseEdit');
    }

    /**
     * 删除
     */
    public function houseDel(){
        $id=input('post.id');
        if (empty($id)){
            return $this->error('大棚不存在!');
        }
        db('nyd_house')->where('id',$id)->delete();
        return $this->success('删除成功');
    }


}